<?php
class Controller_Online_EbayTemplate extends Controller_Abstract
{
	function actionBaseCreate()
	{
		$this->_view['title'] = '新建基板';
	}
	
	function actionBaseCreateSave()
	{
		$this->_view['title'] = '新建基板（保存）';
		if ($this->_context->isPost())
		{
			$base = array('name' => trim($_POST['name']), 'type' => $_POST['type'], 'content' => trim($_POST['content']), 'css' => trim($_POST['css']));
			$result = Ebay_Template_Base::create($base);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirect(url('Online::EbayTemplate/BaseList'));
			}
			else
			{
				return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionBaseList()
	{
		$this->_view['title'] = '基板列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = Ebay_Template_Base::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Page($result['pagination'], url('Online::EbayTemplate/BaseList', $url_arr));
			$this->_view['page'] = $page->getPage();
			$this->_view['base'] = $result['data'];
		}
	}
	
	function actionBaseDetail()
	{
		$this->_view['title'] = '基板详细';
		$id = isset($_GET['id']) ? $_GET['id'] : '';
		if ($id)
		{
			$base = Ebay_Template_Base::find('id=?', $id)->asArray()->getOne();
			if ($base['id'])
			{
				$base['part'] = Ebay_Template_Base_Part::find('ebay_template_base_id=?', $id)->asArray()->getAll();
				foreach ($base['part'] as $k => $bp)
				{
					$base['part'][$k]['module'] = Ebay_Template_Base_Part_Module::find('ebay_template_base_part_id=?', $bp['id'])->asArray()->getAll();
				}
			}
			$this->_view['base'] = $base;
		}
	}
	
	function actionBaseEdit()
	{
		$this->_view['title'] = '基板编辑';
		$id = isset($_GET['id']) ? $_GET['id'] : '';
		if ($id)
		{
			$base = Ebay_Template_Base::find('id=?', $id)->asArray()->getOne();
			$this->_view['base'] = $base;
		}
	}
	
	function actionBaseEditSave()
	{
		$this->_view['title'] = '基板编辑（保存）';
		if ($this->_context->isPOST())
		{
			$base = array('id' => $_POST['id'], 'name' => trim($_POST['name']), 'content' => trim($_POST['content']), 'css' => trim($_POST['css']));
			$result = Ebay_Template_Base::edit($base);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('保存成功', '正在返回..', url('Online::EbayTemplate/BaseList'), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionBaseDelete()
	{
		$this->_view['title'] = '基板删除';
		$id = isset($_GET['id']) ? $_GET['id'] : '';
		if ($id)
		{
			$result = Ebay_Template_Base::delete($id);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('删除成功', '正在返回..', url('Online::EbayTemplate/BaseList'), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('删除失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionBasePartCreate()
	{
		$this->_view['title'] = '新建基板块';
		$base = Ebay_Template_Base::find()->asArray()->setColumns('id, name')->getAll();
		$this->_view['base'] = $base;
	}
	
	function actionBasePartCreateSave()
	{
		$this->_view['title'] = '新建基板块（保存）';
		$base_part = array();
		if ($this->_context->isPOST())
		{
			$base_part = array('name' => trim($_POST['name']), 'variable_name' => trim($_POST['variable_name']), 'content' => trim($_POST['content']), 'ebay_template_base_id' => $_POST['base_id']);
			$result = Ebay_Template_Base_part::create($base_part);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirect(url('Online::EbayTemplate/BaseList'));
			}
			else
			{
				return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionBasePartDelete()
	{
		$this->_view['title'] = '基板块删除';
		$id = isset($_GET['id']) ? $_GET['id'] : '';
		if ($id)
		{
			$result = Ebay_Template_Base_Part::delete($id);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('删除成功', '正在返回..', url('Online::EbayTemplate/BaseDetail', array('id' => $_GET['parent_action_id'])), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('删除失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionBasePartEdit()
	{
		$this->_view['title'] = '基板块编辑';
		$base = Ebay_Template_Base::find()->asArray()->getAll();
		$this->_view['base'] = $base;
		$id = isset($_GET['id']) ? $_GET['id'] : '';
		if ($id)
		{
			$base_part = Ebay_Template_Base_Part::find('id=?', $id)->asArray()->getOne();
			$this->_view['base_part'] = $base_part;
		}
	}
	
	function actionBasePartEditSave()
	{
		$this->_view['title'] = '基板块编辑（保存）';
		if ($this->_context->isPOST())
		{
			$base_part = array('id' => $_POST['id'], 'name' => trim($_POST['name']), 'variable_name' => trim($_POST['variable_name']), 'ebay_template_base_id' => $_POST['base_id'], 'content' => trim($_POST['content']));
			$result = Ebay_Template_Base_Part::edit($base_part);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('保存成功', '正在返回..', url('Online::EbayTemplate/BaseDetail', array('id' => $_POST['base_id'])), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionBasePartModuleCreate()
	{
		$this->_view['title'] = '新建基板块模块';
		$base_part = Ebay_Template_Base_Part::find('ebay_template_base_id=?', $_GET['parent_action_id'])->asArray()->getAll();
		$this->_view['base_part'] = $base_part;
	}
	
	function actionBasePartModuleCreateSave()
	{
		$this->_view['title'] = '新建基板块模块（保存）';
		if ($this->_context->isPOST())
		{
			$module = array('name' => trim($_POST['name']), 'base_part_id' => $_POST['base_part_id'], 'variable_name' => trim($_POST['variable_name']), 'sort' => (int)$_POST['sort'], 'type' => trim($_POST['type']));
			$result = Ebay_Template_Base_Part_Module::create($module);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirect(url('Online::EbayTemplate/BaseDetail', array('id' => $_POST['parent_action_id'])));
			}
			else
			{
				return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionBasePartModuleEdit()
	{
		$this->_view['title'] = '基板块模块编辑';
		$id = isset($_GET['id']) ? $_GET['id'] : '';
		if ($id)
		{
			$base_part_module = Ebay_Template_Base_Part_Module::find('id=?', $id)->asArray()->getOne();
			$base_part = Ebay_Template_Base_Part::find('ebay_template_base_id=?', $_GET['parent_action_id'])->asArray()->getAll();
			$this->_view['base_part'] = $base_part;
			$this->_view['base_part_module'] = $base_part_module;
		}
	}
	
	function actionBasePartModuleEditSave()
	{
		$this->_view['title'] = '基板块模块编辑（保存）';
		if ($this->_context->isPOST())
		{
			$base_part_module = array('id' => $_POST['id'], 'name' => trim($_POST['name']), 'variable_name' => trim($_POST['variable_name']), 'ebay_template_base_part_id' => $_POST['base_part_id'], 'sort' => (int)$_POST['sort'], 'type' => trim($_POST['type']));
			$result = Ebay_Template_Base_Part_Module::edit($base_part_module);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('保存成功', '正在返回..', url('Online::EbayTemplate/BaseDetail', array('id' => $_POST['parent_action_id'])), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionBasePartModuleDelete()
	{
		$this->_view['title'] = '基板块模块删除';
		$id = isset($_GET['id']) ? $_GET['id'] : '';
		if ($id)
		{
			$result = Ebay_Template_Base_Part_Module::delete($id);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('删除成功', '正在返回..', url('Online::EbayTemplate/BaseDetail', array('id' => $_GET['parent_action_id'])), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('删除失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionBaseCopy()
	{
		$this->_view['title'] = '基板复制';
		if ($this->_context->isPOST())
		{
			$template = Ebay_Template_Base::find('ebay_template_base.id=?', $_POST['copy_id'])->asArray()->getOne();
			if ($template)
			{
				$copy_template = array('name' => trim($_POST['template_base_name']), 'type' => $_POST['type'], 'content' => $template['content'], 'css' => $template['css']);
				$result = Ebay_Template_Base::create($copy_template);
				if ($result['ack'] == SUCCESS)
				{
					$template_base_part = Ebay_Template_Base_Part::find('ebay_template_base_id=?', $_POST['copy_id'])->asArray()->getAll();
					foreach($template_base_part as $part)
					{
						$ebay_template_base_part = Ebay_Template_Base_Part::create(array('ebay_template_base_id' => $result['ebay_template_base_id'], 'name' => $part['name'], 'variable_name' => $part['variable_name'], 'content' => $part['content']));
						$template_base_part_module = Ebay_Template_Base_Part_Module::find('ebay_template_base_part_id=?', $part['id'])->asArray()->getAll();
						foreach($template_base_part_module as $part_module)
						{
							Ebay_Template_Base_Part_Module::create(array('base_part_id' => $ebay_template_base_part['ebay_template_base_part_id'], 'name' => $part_module['name'], 'variable_name' => $part_module['variable_name'], 'sort' => $part_module['sort'], 'type' => $part_module['type']));
						}
					}
					$new_template = Ebay_Template_Base::find('ebay_template_base.id=?', $result['ebay_template_base_id'])->asArray()->getOne();
					$source = Q::ini('custom_system/ebay_template_local_dir') . 'base/' . $template['path'];
					$destination = Q::ini('custom_system/ebay_template_local_dir') . 'base/' . $new_template['path'];
					Helper_BSS_Normal::xcopy($source, $destination);
					return $this->_redirectMessage('复制成功', '正在返回..', url('Online::EbayTemplate/BaseList'), $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('复制失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
		}
	}
	
	function actionCatalogue()
	{
		$this->_view['title'] = '上传基板文件';
		$id = isset($_GET['id']) ? $_GET['id'] : '';
		if ($id)
		{
			$base = Ebay_Template_Base::find('id=?', $id)->asArray()->getOne();
			if ($base['id'])
			{
				$base['part'] = Ebay_Template_Base_Part::find('ebay_template_base_id=?', $id)->asArray()->getAll();
				foreach ($base['part'] as $k => $bp)
				{
					$base['part'][$k]['module'] = Ebay_Template_Base_Part_Module::find('ebay_template_base_part_id=?', $bp['id'])->asArray()->getAll();
				}
			}
			$this->_view['base'] = $base;
		}
	}
	
	function actionFileRename()
	{
		$this->_view['title'] = '文件（文件夹）重命名';
		$newpath = implode('/',explode('/', $_POST['path'], -1)) . '/' . $_POST['newname'];
		rename($_POST['path'], $newpath);
		if (file_exists($newpath))
		{
			echo "重命名成功";
		}
		else
		{
			echo "重命名失败";
		}
		exit;
	}
	
	function actionCreateCatalogue()
	{
		$this->_view['title'] = '创建目录';
		if (is_dir($_POST['path'] . '/' . $_POST['name']))
		{
			echo "文件夹已存在";exit;
		}
		else
		{
			mkdir($_POST['path'] . '/' . $_POST['name']);
			if (is_dir($_POST['path'] . '/' . $_POST['name']))
			{
				echo "创建文件夹成功";
			}
			else
			{
				echo "创建文件夹失败";
			}
		}
		exit;
	}
	
	function actionDeleteBaseFile()
	{
		$this->_view['title'] = '删除基板文件';
		if (is_file($_POST['path']))
		{
			unlink($_POST['path']);
		}
		else
		{
			Helper_BSS_Normal::remove_directory($_POST['path']);
		}
		if (!file_exists($_POST['path']))
		{
			echo "删除成功";
		}
		else
		{
			echo "删除失败";
		}
		exit;
	}
	
	function actionUploadBase()
	{
		$this->_view['title'] = '上传基板文件（保存）';
		if ($this->_context->isPOST()) 
		{
			if (!$_FILES) { echo "请选择文件。";exit; }
			$path = $_POST['path'];
	        $ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp','html','text','txt','rar','css','js','woff','ttf','svg','eot');
	        $max_size = 1000000;
	        $message = '';
	        for($i=0; $i<count($_FILES['fileList']['name']); $i++)
	        {
	        	$file_name = $_FILES['fileList']['name'][$i];
	        	$tmp_name = $_FILES['fileList']['tmp_name'][$i];
	        	$file_size = $_FILES['fileList']['size'][$i];
				$temp_arr = explode(".", $file_name);
				$file_ext = array_pop($temp_arr);
				$file_ext = trim($file_ext);
				$file_ext = strtolower($file_ext);
				if (@is_writable($path) === false) { $message .= $path . "上传目录没有写权限。"; }
		        if (@is_uploaded_file($tmp_name) === false) { $message .= "临时文件可能不是上传文件。"; }
		        if ($file_size > $max_size) { $message .= $file_name . "上传文件大小超过限制。"; }
	            if (in_array($file_ext, $ext_arr) === false) { $message .= $file_ext . "为不允许的文件类型"; }
	       		else if (move_uploaded_file($tmp_name, $path . '/' . $file_name) === false)
	       		{
	       			$message .= 'error';
	       		}
	        }
	        if($message)
	        {
	        	echo "部分上传失败!" . $message; 
	        }
	        else
	        {
	        	echo "上传成功!";
	        }
	        exit;
		}		
	}
	
	function actionTemplateCreate()
	{
		$this->_view['title'] = '新建模板';
		$this->_view['shop'] = Shop::getPlatformShopToArray();
		$base = Ebay_Template_Base::find()->asArray()->getAll();
		foreach ($base as $k => $b)
		{
			$base[$k]['module'] = Ebay_Template_Base_Part_Module::find('ebay_template_base_part_module.ebay_template_base_part_id=ebay_template_base_part.id')
				->joinLeft('ebay_template_base_part', '', 'ebay_template_base_part.ebay_template_base_id='.$b['id']) 
				->asArray()->getAll();
			$this->_view['base'] = $base;
		}
	}
	
	function actionTemplateCreateSave()
	{
		$this->_view['title'] = '新建模板（保存）';
		if ($this->_context->isPOST())
		{
			$template = array('name' => trim($_POST['name']), 'shop_id' => $_POST['shop_id'], 'ebay_template_base_id' => $_POST['base_id'], 'css' => trim($_POST['css']), 'base_part_module_ids' => (isset($_POST['base_part_module_ids']) ? $_POST['base_part_module_ids'] : array()));
			$result = Ebay_Template::create($template);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('保存成功', '正在返回..', url('Online::EbayTemplate/TemplateList'), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionTemplateEdit()
	{
		$this->_view['title'] = '模板编辑';
		$this->_view['shop'] = Shop::getPlatformShopToArray();
		if (isset($_GET['id']))
		{
			$template = Ebay_Template::find('ebay_template.id=?', $_GET['id'])
				->joinLeft('ebay_template_base', 'name as base_name', 'ebay_template_base.id=ebay_template.ebay_template_base_id')
				->asArray()->getOne();
			$template['ebay_template_base_part_module_ids'] = QDB::getConn()->execute('select ebay_template_base_part_module_id from ebay_template_module where ebay_template_id='.$template['id'])->fetchCol('ebay_template_base_part_module_id');
			$this->_view['template'] = $template;
			$module = array();
			$part = Ebay_Template_Base_Part::find('ebay_template_base_id=?', $template['ebay_template_base_id'])->asArray()->getAll();
			foreach ($part as $p)
			{
				$module[$p['name']] = Ebay_Template_Base_Part_Module::find('ebay_template_base_part_id=?', $p['id'])->asArray()->getAll();
			}
			$this->_view['module'] = $module;
		}
	}
	
	function actionTemplateEditSave()
	{
		$this->_view['title'] = '模板编辑（保存）';
		if ($this->_context->isPOST())
		{
			$template = array('id' => $_POST['id'] ,'name' => trim($_POST['name']), 'base_part_module_ids' => (isset($_POST['base_part_module_ids']) ? $_POST['base_part_module_ids'] : array()), 'css' => trim($_POST['css']));
			$result = Ebay_Template::edit($template);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('编辑成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('编辑失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionTemplateList()
	{
		$this->_view['title'] = '模板列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 15;
		$shop = Shop::getPlatformShopToArray();
		$this->_view['shop'] = $shop;
		$base = Ebay_Template_Base::find()->asArray()->getAll();
		$this->_view['base'] = $base;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = Ebay_Template::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Page($result['pagination'], url('Online::EbayTemplate/TemplateList', $url_arr));
			$this->_view['page'] = $page->getPage();
			$this->_view['template'] = $result['data'];
		}
	}
	
	function actionTemplateDetail()
	{
		$this->_view['title'] = '模板详细';
		if ($_GET['id'])
		{
			$template = Ebay_Template::find('ebay_template.id=?', $_GET['id'])->joinLeft('shop', 'name as shop_name, path as shop_path', 'shop.id=ebay_template.shop_id')->asArray()->getOne();
			$template['module'] = Ebay_Template_Base_Part_Module::find('ebay_template_base_part_module.id=ebay_template_module.ebay_template_base_part_module_id')
				->joinLeft('ebay_template_module', 'id as ebay_template_module_id', 'ebay_template_module.ebay_template_id='.$template['id'])
				->asArray()->getAll();
			$this->_view['template'] =$template;
			if ($template['module'])
			{
				$default_module_id = isset($_GET['mid']) ? $_GET['mid'] : $template['module'][0]['ebay_template_module_id'];
				$content = Ebay_Template_Module_Content::find('ebay_template_module_content.ebay_template_module_id=ebay_template_module.id')
					->joinLeft('ebay_template_module', '', 'ebay_template_module.id='.$default_module_id)
					->joinLeft('ebay_template_base_part_module', 'name as module_name', 'ebay_template_base_part_module.id=ebay_template_module.ebay_template_base_part_module_id')
					->order('ebay_template_module_content.sort asc')->asArray()->getAll();
				$this->_view['content'] = $content;
			}
		}
	}

	function actionTemplateModuleContentEdit()
	{
		$this->_view['title'] = '模板块内容编辑';
		if (isset($_GET['id']))
		{
			$template = Ebay_Template::find('ebay_template.id=?', $_GET['id'])->joinLeft('shop', 'name as shop_name, path as shop_path', 'shop.id=ebay_template.shop_id')->asArray()->getOne();
			$this->_view['template'] = $template;
			if ($template)
			{
				$module = Ebay_Template_Base_Part_Module::find('ebay_template_base_part_module.id=ebay_template_module.ebay_template_base_part_module_id')
					->joinLeft('ebay_template_module', 'id as ebay_template_module_id', 'ebay_template_module.ebay_template_id='.$template['id'])
					->asArray()->getAll();
				if ($module)
				{
					$default_module_id = isset($_GET['mid']) ? $_GET['mid'] : $module[0]['ebay_template_module_id'];
					$default_module = Ebay_Template_Module::find('ebay_template_module.id=?', $default_module_id)
						->joinLeft('ebay_template_base_part_module', 'name as module_name', 'ebay_template_base_part_module.id=ebay_template_module.ebay_template_base_part_module_id')
						->asArray()->getOne();
					$this->_view['module'] = $default_module;
					if ($default_module)
					{
						$content = Ebay_Template_Module_Content::find('ebay_template_module_content.ebay_template_module_id=ebay_template_module.id')
							->joinLeft('ebay_template_module', '', 'ebay_template_module.id='.$default_module['id'])
							->joinLeft('ebay_template_base_part_module', 'name as module_name', 'ebay_template_base_part_module.id=ebay_template_module.ebay_template_base_part_module_id')
							->order('ebay_template_module_content.sort asc')->asArray()->getAll();
						$this->_view['content'] = $content;
					}
				}
			}
		}
	}
	
	function actionTemplateModuleContentEditSave()
	{
		$this->_view['title'] = '模板块内容编辑（保存）';
		if ($this->_context->isPOST())
		{
			$content = array();
			foreach ($_POST as $key => $value)
			{
				$k = explode('_', $key);
				if (! array_key_exists($k[1], $content))
				{
					$content[$k[1]]['ebay_template_module_id'] = $_POST['mid_'.$k[1]];
					$content[$k[1]]['title'] = trim($_POST['title_'.$k[1]]);
					$content[$k[1]]['image'] = trim($_POST['image_'.$k[1]]);
					$content[$k[1]]['price'] = trim($_POST['price_'.$k[1]]);
					$content[$k[1]]['url'] = trim($_POST['url_'.$k[1]]);
					$content[$k[1]]['content'] = trim($_POST['content_'.$k[1]]);
					$content[$k[1]]['sort'] = trim($_POST['sort_'.$k[1]]);
					$default_module_id = $_POST['mid_'.$k[1]];
				}
			}
			$result = Ebay_Template_Module_Content::update($content, $default_module_id);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('修改完成', '返回列表', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			if ($result['ack'] == PARTIAL_FAILURE)
			{
				return $this->_redirectMessage('修改失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionUpload()
	{
		$this->_view['title'] = '上传文件';
		if (isset($_GET['id']))
		{
			$template = Ebay_Template::find('ebay_template.id=?', $_GET['id'])->joinLeft('shop', 'name as shop_name, path as shop_path', 'shop.id=ebay_template.shop_id')->asArray()->getOne();
			$this->_view['template'] = $template;
		}
	}
	
	function actionUploadSave()
	{
		$this->_view['title'] = '上传文件（保存）';
		$result = Helper_BSS_Normal::upload($_FILES['Filedata'], Q::ini('custom_system/ebay_template_local_dir'). $_POST['path']);
		echo $result['message'];exit;
	}
	
	function actionFilesListDelete()
	{
		$this->_view['title'] = '删除目录文件';
		if ($_REQUEST['path'] && $_REQUEST['shop_path'] && $_REQUEST['filename'])
		{
			unlink(Q::ini('custom_system/ebay_template_local_dir') . $_REQUEST['shop_path'] . '/' . $_REQUEST['path'] . '/' . $_REQUEST['filename']);
			return $this->_redirect($_SERVER['HTTP_REFERER']);
		}
	}
	
	function actionRefreshFilesList()
	{
		$this->_view['title'] = 'ajax刷新文件列表';
		echo json_encode(array_chunk(Helper_BSS_Normal::getDirlist(Q::ini('custom_system/ebay_template_local_dir') . $_REQUEST['shop_path'] . '/' . $_REQUEST['template_path']), 27));exit;
	}
	
	function actionTemplatePreview()
	{
		$this->_view['title'] = '模板预览';
		if ($_GET['id'])
		{
			$result = Ebay_Template::getTemplatePreview($_GET['id'], $_GET['type']);
			if ($result['ack'] == SUCCESS)
			{
				if (isset($_GET['getcode']) && $_GET['getcode'] == true)
				{
					$this->_view['contents'] = $result['response'];
				}
				else
				{
					echo $result['response'];exit;
				}
			}
			else
			{
				return $this->_redirectMessage('预览失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}

		}
	}
	
	function actionSynOutside()
	{
		$this->_view['title'] = '同步外网';
		if ($_GET['id'])
		{
			$template = Ebay_Template::find('ebay_template.id=?', $_GET['id'])
				->joinLeft('ebay_template_base', 'path as base_path, content as base_content, css as base_css', 'ebay_template_base.id=ebay_template.ebay_template_base_id')
				->joinLeft('shop', 'name as shop_name, path as shop_path, link as shop_link', 'shop.id=ebay_template.shop_id')
				->asArray()->getOne();
			if ($template)
			{
				//copy基板文件目录
				$source = Q::ini('custom_system/ebay_template_local_dir');
				$destination = Q::ini('custom_system/ebay_template_prodcution_for_outside_dir');
				Helper_BSS_Normal::xcopy($source . Q::ini('custom_system/ebay_base_template_relative_path') . $template['base_path'], $destination . Q::ini('custom_system/ebay_base_template_relative_path') . $template['base_path']);
				Helper_BSS_Normal::xcopy($source . $template['shop_path'] . '/' . $template['path'], $destination . $template['shop_path'] . '/' . $template['path']);
				
				//覆盖模板旧文件
				$vars['template']['base_url'] = Q::ini('custom_system/ebay_template_outside_base_url') . $template['shop_path'] . '/' . $template['path'] . '/';
				$vars['template']['shop_name'] = $template['shop_name'];
				$vars['template']['shop_link'] = $template['shop_link'];
				$vars['base']['base_url'] = Q::ini('custom_system/ebay_template_outside_base_url') . Q::ini('custom_system/ebay_base_template_relative_path') . $template['base_path'] . '/';
				$vars['base']['part'] = Ebay_Template_Base_Part::find('ebay_template_base_id=?', $template['ebay_template_base_id'])->asArray()->getAll();
				foreach ($vars['base']['part'] as $bp)
				{
					$base_module = Ebay_Template_Base_Part_Module::find('ebay_template_base_part_id=?', $bp['id'])->asArray()->getAll();
					foreach ($base_module as $bm)
					{
						$template_module_content = Ebay_Template_Module_Content::find('ebay_template_module_content.ebay_template_module_id=ebay_template_module.id')
							->joinLeft('ebay_template_module', '', 'ebay_template_module.ebay_template_id=' . $template['id'] . ' and ebay_template_module.ebay_template_base_part_module_id=' . $bm['id'])
							->order('ebay_template_module_content.sort asc')->asArray()->getAll();
						if ($template_module_content)
						{
							$vars[$bm['variable_name']] = $template_module_content;
						}
					}
					$html = Helper_BSS_Ebtpl::parseToHtml($bp['content'], $vars);
					$html = Helper_BSS_Ebtpl::parseToHtml($html, $vars);
					file_put_contents(Q::ini('custom_system/ebay_template_prodcution_for_outside_dir') . $template['shop_path'] . '/' . $template['path'] . '/' . $bp['variable_name'].'.html', $html);
				}
			}
			Helper_BSS_API::ebayTemplateRsync();
			return $this->_redirectMessage('外网生成成功', '正在跳转', $_SERVER['HTTP_REFERER'], SUCCESS);
		}
	}
	
	function actionTemplateCopy()
	{
		$this->_view['title'] = '模板复制';
		if ($this->_context->isPOST())
		{
			$template = Ebay_Template::find('ebay_template.id=?', $_POST['copy_id'])
				->joinLeft('shop', 'path as shop_path', 'shop.id=ebay_template.shop_id')
				->asArray()->getOne();
			if ($template)
			{
				$base_part_module_ids = QDB::getConn()->execute('select ebay_template_base_part_module_id from ebay_template_module where ebay_template_id=' . $template['id'])->fetchCol();
				$copy_template = array('name' => trim($_POST['template_name']), 'shop_id' => $_POST['shop_id'], 'ebay_template_base_id' => $template['ebay_template_base_id'], 'css' => $template['css'], 'base_part_module_ids' => $base_part_module_ids);
				$result = Ebay_Template::create($copy_template);
				if ($result['ack'] == SUCCESS)
				{
					foreach ($base_part_module_ids as $id)
					{
						$template_module_content = Ebay_Template_Module_Content::find('ebay_template_module_content.ebay_template_module_id=ebay_template_module.id')
							->joinLeft('ebay_template_module', '', 'ebay_template_module.ebay_template_id=' . $template['id'] . ' and ebay_template_module.ebay_template_base_part_module_id=' . $id)
							->asArray()->getAll();
						$copy_module = Ebay_Template_Module::find('ebay_template_id=? and ebay_template_base_part_module_id=?', $result['ebay_template_id'], $id)->asArray()->getOne();
						Ebay_Template_Module_Content::update($template_module_content, $copy_module['id']);
					}
					$new_template = Ebay_Template::find('ebay_template.id=?', $result['ebay_template_id'])
						->joinLeft('shop', 'path as shop_path', 'shop.id=ebay_template.shop_id')
						->asArray()->getOne();
					$source = Q::ini('custom_system/ebay_template_local_dir') . $template['shop_path'] . '/' . $template['path'];
					$destination = Q::ini('custom_system/ebay_template_local_dir') . $new_template['shop_path'] . '/' . $new_template['path'];
					Helper_BSS_Normal::xcopy($source, $destination);
					return $this->_redirectMessage('复制成功', '正在返回..', url('Online::EbayTemplate/TemplateList'), $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('复制失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
		}
	}
}